package com.itheima.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
@RequestMapping("/employee")
@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    //登录
    @PostMapping("/login")
    public R<Employee> Login(@RequestBody Employee employee, HttpSession session){
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(queryWrapper);

        if(one == null){
            return R.error("登录失败");
        }

        if(!one.getPassword().equals(password)){
            return R.error("登录失败");
        }
        if(one.getStatus() == 0){
            return R.error("账号已禁用");
        }
        session.setAttribute("employee",one.getId());
        one.setPassword(null);
        return R.success(one);
    }


    //退出登录
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        session.removeAttribute("employee");
        return R.success("退出成功");

    }


    //添加用户
    @PostMapping
    public R<String> Add(@RequestBody Employee employee, HttpSession session){
        //Long employId = (Long) session.getAttribute("employee");
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        // employee.setCreateTime(LocalDateTime.now());
        // employee.setCreateUser(employId);
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser(employId);
        boolean save = employeeService.save(employee);
        return save? R.success("新增成功"):R.error("新增失败");
    }


    //分页查询
    @GetMapping("/page")
    public R<Page> page(String name, int page,int pageSize){
        Page pageInfo=new Page(page,pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        Page page1 = employeeService.page(pageInfo, queryWrapper);
        return R.success(page1);
    }



    //编辑用户信息
    @GetMapping("/{id}")
    public R<Employee> edit(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }




    //根据员工id进行修改
    @PutMapping
    public R<String>  update(HttpSession session, @RequestBody Employee employee ){
        //Long empId = (Long) session.getAttribute("employee");
        // employee.setUpdateUser(empId);
        // employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("修改员工信息成功");
    }
}